# -*- coding: utf-8 -*-

import os
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QTextEdit, QGridLayout, QLabel
from PyQt5.QtGui import QFont, QPixmap
from PyQt5.QtCore import QEvent, Qt

from flow.check.checked_backend_processor import CheckedBackendProcessor


class CheckWindow(QWidget):
    def __init__(self, src_folder, dst_folder):
        super(CheckWindow, self).__init__()
        self.status_line_edit = QTextEdit()
        self.previous_image_button = QPushButton("返回上一张")
        self.next_image_button = QPushButton("进入下一张")
        self.image_label = QLabel()
        self.backend_processor = CheckedBackendProcessor(src_folder, dst_folder)

        self.previous_image_button.clicked.connect(self.previous_image_button_pressed)
        self.next_image_button.clicked.connect(self.next_image_button_pressed)

        self.init_ui()
        self.set_up_child_widgets_property()
        self.setGeometry(300, 300, 1000, 550)
        self.begin_label()
        self.show()

    def begin_label(self):
        processed_count, left_count = self.backend_processor.load_an_image()
        if processed_count > 0:
            self.previous_image_button.setDisabled(False)
        if left_count > 0:
            self.next_image_button.setDisabled(False)
            self.image_label.setPixmap(QPixmap(self.backend_processor.draw_image()))
        self.show_label_status(processed_count, left_count)

    def init_ui(self):
        font = QFont("宋体", 15)
        font.setBold(True)
        self.status_line_edit.setFixedHeight(40)
        self.previous_image_button.setFixedHeight(40)
        self.previous_image_button.setFont(font)
        self.next_image_button.setFixedHeight(40)
        self.next_image_button.setFont(font)
        grid_layout = QGridLayout()
        grid_layout.addWidget(self.status_line_edit, 0, 0, 1, 8)
        grid_layout.addWidget(self.previous_image_button, 0, 8, 1, 2)
        grid_layout.addWidget(self.next_image_button, 0, 10, 1, 2)
        grid_layout.setSpacing(15)
        grid_layout.addWidget(self.image_label, 2, 0, 2, 12)
        self.setLayout(grid_layout)
        self.setWindowTitle("客流检查工具")

    def set_up_child_widgets_property(self):
        self.status_line_edit.setDisabled(True)
        self.previous_image_button.setDisabled(True)
        self.next_image_button.setDisabled(True)
        self.image_label.setScaledContents(True)
        self.image_label.installEventFilter(self)
        self.image_label.setFocus()

    def show_label_status(self, processed_count, left_count):
        text = "<span style=\"font-size:18pt; font-weight:600; color:#ff0000;\">"
        text += "正在检查。。。"
        text += "</span>"
        text += "<span style=\"font-size:18pt; font-weight:600; color:#000000;\">"
        text += "已经检查了 "
        text += "</span>"
        text += "<span style=\"font-size:18pt; font-weight:600; color:#0000ff;\">"
        text += "%d" % processed_count
        text += "</span>"
        text += "<span style=\"font-size:18pt; font-weight:600; color:#000000;\">"
        text += " 张图片"
        text += "</span>"
        text += "<span style=\"font-size:18pt; font-weight:600; color:#000000;\">"
        text += ", 还剩余 "
        text += "</span>"
        text += "<span style=\"font-size:18pt; font-weight:600; color:#0000ff;\">"
        text += "%d" % left_count
        text += "</span>"
        text += "<span style=\"font-size:18pt; font-weight:600; color:#000000;\">"
        text += " 张图片"
        text += "</span>"
        self.status_line_edit.setText(text)

    def eventFilter(self, source, event):
        if source is not self.image_label:
            return QWidget.eventFilter(self, source, event)
        if event.type() != QEvent.MouseButtonPress and event.type() != QEvent.KeyPress:
            return QWidget.eventFilter(self, source, event)
        if event.type() == QEvent.KeyPress:
            if event.key() == Qt.Key_Escape:
                self.next_image_button_pressed()
            return QWidget.eventFilter(self, source, event)
        if event.button() == Qt.LeftButton:
            x = event.x() - 5 if event.x() - 5 > 0 else 0
            y = event.y() - 5 if event.y() - 5 > 0 else 0
            relative_position = [x / self.image_label.width(), y / self.image_label.height()]
            self.backend_processor.left_mouse_button_event(relative_position)
            self.image_label.setPixmap(QPixmap(self.backend_processor.draw_image()))
        if event.button() == Qt.RightButton:
            x = event.x() - 5 if event.x() - 5 > 0 else 0
            y = event.y() - 5 if event.y() - 5 > 0 else 0
            relative_position = [x / self.image_label.width(), y / self.image_label.height()]
            self.backend_processor.right_mouse_button_event(relative_position)
            self.image_label.setPixmap(QPixmap(self.backend_processor.draw_image()))
        return QWidget.eventFilter(self, source, event)

    def previous_image_button_pressed(self):
        processed_count, left_count = self.backend_processor.previous_image_button_event()
        if processed_count == 0:  # 目标文件夹已经没有标注过的图片
            self.previous_image_button.setDisabled(True)
        if left_count > 0:
            self.next_image_button.setDisabled(False)
            self.image_label.setPixmap(QPixmap(self.backend_processor.draw_image()))
        self.show_label_status(processed_count, left_count)
        self.image_label.setFocus()

    def next_image_button_pressed(self):
        processed_count, left_count = self.backend_processor.next_image_button_event()
        if left_count == 0:  # 所有图片已经标注完毕
            self.next_image_button.setDisabled(True)
            self.image_label.clear()
        else:  # 显示下一张图片
            self.next_image_button.setDisabled(False)
            self.image_label.setPixmap(QPixmap(self.backend_processor.draw_image()))
        if processed_count > 0:
            self.previous_image_button.setDisabled(False)
        self.show_label_status(processed_count, left_count)
        self.image_label.setFocus()


def main(src_folder):
    current_folder = os.getcwd()
    # src_folder = os.path.join(current_folder, "labeled")
    folder_name = os.path.basename(src_folder)
    dst_folder = os.path.join(current_folder, folder_name + "_checked")
    if not os.path.isdir(src_folder):
        os.mkdir(src_folder)
    try:
        os.mkdir(dst_folder)
    except WindowsError:
        pass
    app = QApplication(sys.argv)
    check_window = CheckWindow(src_folder, dst_folder)
    check_window.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    if len(sys.argv) < 2:
        exit(1)
    main(sys.argv[1])
